<template>
  <view class="forgot-password-container">
    <!-- 页面头部 -->
    <view class="page-header">
      <image class="back-icon" src="/static/Profile/箭头-左.png" mode="aspectFit" @tap="goBack"></image>
      <text class="page-title">忘记密码</text>
    </view>

    <!-- 重置密码表单 -->
    <view class="reset-form">
      <!-- 手机号输入 -->
      <view class="form-item">
        <view class="input-wrapper">
          <image class="input-icon" src="/static/Dl/手机.png" mode="aspectFit"></image>
          <input 
            class="form-input" 
            type="text" 
            placeholder="请输入绑定的手机号" 
            v-model="formData.phone"
            maxlength="11"
            placeholder-style="color: #cccccc;"
          />
        </view>
      </view>
      
      <!-- 验证码输入 -->
      <view class="form-item">
        <view class="input-wrapper verify-wrapper">
          <image class="input-icon" src="/static/Dl/验证码.png" mode="aspectFit"></image>
          <input 
            class="form-input" 
            type="text" 
            placeholder="请输入验证码" 
            v-model="formData.code"
            maxlength="6"
            placeholder-style="color: #cccccc;"
          />
          <!-- 发送验证码按钮 -->
          <button 
            class="verify-btn" 
            @tap="sendVerifyCode"
            :disabled="isSendingCode"
          >
            <template v-if="isSendingCode">{{ countDown }}s后重新发送</template>
            <template v-else>获取验证码</template>
          </button>
        </view>
      </view>
      
      <!-- 新密码输入 -->
      <view class="form-item">
        <view class="input-wrapper">
          <image class="input-icon" src="/static/Dl/密码 .png" mode="aspectFit"></image>
          <input 
            class="form-input" 
            :type="showPassword ? 'text' : 'password'" 
            placeholder="请设置新密码（6-20位）" 
            v-model="formData.newPassword"
            placeholder-style="color: #cccccc;"
          />
          <image 
            class="eye-icon" 
            :src="showPassword ? '/static/Dl/睁眼.png' : '/static/Dl/闭眼.png'"
            @tap="togglePassword"
            mode="aspectFit"
          ></image>
        </view>
      </view>
      
      <!-- 确认密码输入 -->
      <view class="form-item">
        <view class="input-wrapper">
          <image class="input-icon" src="/static/Dl/密码 .png" mode="aspectFit"></image>
          <input 
            class="form-input" 
            :type="showConfirmPassword ? 'text' : 'password'" 
            placeholder="请再次输入新密码" 
            v-model="formData.confirmPassword"
            placeholder-style="color: #cccccc;"
          />
          <image 
            class="eye-icon" 
            :src="showConfirmPassword ? '/static/Dl/睁眼.png' : '/static/Dl/闭眼.png'"
            @tap="toggleConfirmPassword"
            mode="aspectFit"
          ></image>
        </view>
      </view>
      
      <!-- 提交按钮 -->
      <view class="form-item form-action">
        <button class="submit-btn" @tap="handleResetPassword" :disabled="isSubmitting">
          <template v-if="isSubmitting">提交中...</template>
          <template v-else>确认重置</template>
        </button>
      </view>
    </view>
    
    <!-- 底部跳转登录 -->
    <view class="login-link">
      <text class="link-text">记得密码了？</text>
      <text class="nav-link" @tap="goToLogin">立即登录</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onUnmounted, ref } from 'vue'

interface FormData {
  phone: string
  code: string
  newPassword: string
  confirmPassword: string
}

// 表单数据
const formData = ref<FormData>({
  phone: '',
  code: '',
  newPassword: '',
  confirmPassword: ''
})

// 密码显示状态
const showPassword = ref(false)
const showConfirmPassword = ref(false)
// 按钮状态
const isSendingCode = ref(false)
const isSubmitting = ref(false)
const countDown = ref(60)
let countDownTimer: NodeJS.Timeout | null = null

/**
 * 切换新密码显示状态
 */
const togglePassword = () => {
  showPassword.value = !showPassword.value
}

/**
 * 切换确认密码显示状态
 */
const toggleConfirmPassword = () => {
  showConfirmPassword.value = !showConfirmPassword.value
}

/**
 * 返回上一页
 */
const goBack = () => {
  uni.navigateBack({
    delta: 1
  })
}

/**
 * 跳转到登录页
 */
const goToLogin = () => {
  uni.redirectTo({
    url: '/pages/login'
  })
}

/**
 * 发送验证码
 */
const sendVerifyCode = () => {
  const phone = formData.value.phone.trim()
  
  // 手机号校验
  if (!/^1[3-9]\d{9}$/.test(phone)) {
    uni.showToast({
      title: '请输入11位有效手机号',
      icon: 'none',
      duration: 1500
    })
    return
  }
  
  // 模拟发送验证码请求
  isSendingCode.value = true
  uni.showToast({
    title: '验证码已发送',
    icon: 'none',
    duration: 1500
  })
  
  // 倒计时逻辑
  countDownTimer = setInterval(() => {
    countDown.value--
    if (countDown.value <= 0) {
      clearInterval(countDownTimer as NodeJS.Timeout)
      countDownTimer = null
      isSendingCode.value = false
      countDown.value = 60
    }
  }, 1000)
  
  // 实际项目中替换为真实接口请求
  // uni.request({
  //   url: '/api/send-sms',
  //   method: 'POST',
  //   data: { phone },
  //   success: (res) => {
  //     if (res.data.success) {
  //       uni.showToast({ title: '验证码已发送' })
  //     } else {
  //       uni.showToast({ title: res.data.msg, icon: 'none' })
  //       clearInterval(countDownTimer)
  //       isSendingCode.value = false
  //       countDown.value = 60
  //     }
  //   },
  //   fail: () => {
  //     uni.showToast({ title: '发送失败，请重试', icon: 'none' })
  //     clearInterval(countDownTimer)
  //     isSendingCode.value = false
  //     countDown.value = 60
  //   }
  // })
}

/**
 * 确认重置密码
 */
const handleResetPassword = async () => {
  const { phone, code, newPassword, confirmPassword } = formData.value
  const trimPhone = phone.trim()
  const trimCode = code.trim()
  const trimPwd = newPassword.trim()
  const trimConfirmPwd = confirmPassword.trim()
  
  // 表单校验
  if (!/^1[3-9]\d{9}$/.test(trimPhone)) {
    uni.showToast({ title: '请输入11位有效手机号', icon: 'none', duration: 1500 })
    return
  }
  
  if (trimCode.length !== 6) {
    uni.showToast({ title: '请输入6位验证码', icon: 'none', duration: 1500 })
    return
  }
  
  if (trimPwd.length < 6 || trimPwd.length > 20) {
    uni.showToast({ title: '密码长度需在6-20位之间', icon: 'none', duration: 1500 })
    return
  }
  
  if (trimPwd !== trimConfirmPwd) {
    uni.showToast({ title: '两次输入的密码不一致', icon: 'none', duration: 1500 })
    return
  }
  
  // 防止重复提交
  if (isSubmitting.value) return
  isSubmitting.value = true
  
  try {
    // 模拟重置密码接口请求
    await new Promise((resolve) => setTimeout(resolve, 1500))
    
    // 重置成功
    uni.showToast({
      title: '密码重置成功',
      icon: 'success',
      duration: 1500
    })
    
    // 跳转登录页
    setTimeout(() => {
      goToLogin()
    }, 1500)
  } catch (error) {
    uni.showToast({
      title: '重置失败，请重试',
      icon: 'none',
      duration: 1500
    })
    console.error('密码重置失败：', error)
  } finally {
    isSubmitting.value = false
  }
}

// 页面卸载时清除定时器
onUnmounted(() => {
  if (countDownTimer) {
    clearInterval(countDownTimer)
  }
})
</script>

<style lang="css" scoped>
/* 容器样式 */
.forgot-password-container {
  padding: 0 30rpx;
  background-color: #ffffff;
  min-height: 100vh;
  box-sizing: border-box;
}

/* 页面头部样式 */
.page-header {
  display: flex;
  align-items: center;
  margin-top: 60rpx;
  margin-bottom: 80rpx;
}

.back-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 20rpx;
  cursor: pointer;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
}

/* 表单样式 */
.reset-form {
  margin-bottom: 60rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  height: 100rpx;
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 0 30rpx;
}

.verify-wrapper {
  padding-right: 15rpx;
}

.input-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 20rpx;
}

.form-input {
  flex: 1;
  height: 100%;
  font-size: 30rpx;
  color: #333333;
  background-color: transparent;
  border: none;
  outline: none;
}

.eye-icon {
  width: 40rpx;
  height: 40rpx;
  cursor: pointer;
}

/* 验证码按钮样式 */
.verify-btn {
  width: 180rpx;
  height: 70rpx;
  background-color: #fd091f;
  color: white;
  font-size: 24rpx;
  border-radius: 12rpx;
  border: none;
  margin-left: 15rpx;
}

.verify-btn:disabled {
  background-color: #fd091f;
  color: #ffffff;
  opacity: 0.8;
}

.verify-btn:active {
  opacity: 0.8;
}

/* 提交按钮样式 */
.form-action {
  margin-top: 50rpx;
}

.submit-btn {
  width: 100%;
  height: 100rpx;
  background: linear-gradient(90deg, #ff6b35, #fd091f);
  border-radius: 50rpx;
  color: white;
  font-size: 32rpx;
  font-weight: bold;
  border: none;
}

.submit-btn:active {
  opacity: 0.8;
}

.submit-btn:disabled {
  background: #ffccb3;
  opacity: 0.8;
}

/* 底部登录链接 */
.login-link {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 40rpx;
}

.link-text {
  font-size: 28rpx;
  color: #999999;
  margin-right: 10rpx;
}

.nav-link {
  font-size: 28rpx;
  color: #ff6b35;
  cursor: pointer;
}
</style>